<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace process;

use Workerman\Connection\TcpConnection;

class Websocket
{
    /**
     * 当连接建立时触发的回调函数
     */
    public function onConnect(TcpConnection $connection)
    {
    }

    public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
    {
        if (empty($_SERVER)) $connection->close();
        $HTTP_ORIGIN = $_SERVER['HTTP_ORIGIN'] ?? '';
        if ($HTTP_ORIGIN) {
            if ($HTTP_ORIGIN != WEB_URL) $connection->close();
        }
    }

    /**
     * 收到信息
     */
    public function onMessage(TcpConnection $connection, $data)
    {
        $connection->send(json_encode($data) . 'owen');
    }

    /**
     * 当连接断开时触发的回调函数
     */
    public function onClose(TcpConnection $connection)
    {
        $connection->close();
    }

    /**
     * 当客户端的连接上发生错误时触发
     */
    public function onError(TcpConnection $connection)
    {

    }
}
